unit frmInfo;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, JvExComCtrls, JvListView;

type
  TInfoFRM = class(TForm)
    lvMain: TJvListView;
    procedure FormShow(Sender: TObject);
    procedure lvMainCustomDrawSubItem(Sender: TCustomListView;
      Item: TListItem; SubItem: Integer; State: TCustomDrawState;
      var DefaultDraw: Boolean);
  private
    { Private declarations }
    _vKind: string;

    procedure _FillData;   //connessione al db e recupero dei dati

  public
    { Public declarations }
    property _pKind: string read _vKind write _vKind;
  end;

var
  InfoFRM: TInfoFRM;

implementation

uses frmMain;

{$R *.dfm}

{ TInfoFRM }
//----------------------------------------------------------------------------------------------------//
procedure TInfoFRM._FillData;
  var
    _lCol: TListColumn; //creazione colonne
    _lRow: TListItem;   //dati x le righe
    _lSQLString: string;
    _lCompanyCode: string; //codice DB azienda iScala
    _lRunSum: Integer;  //calcolo della sommatoria dei movimenti
  begin
    _lCompanyCode := MainFRM._ReadINI('GENERAL','CompanyCode');
    _lRunSum := 0; //init

    //in base al parametro della property _vKind selezionno se mostrare i dati della
    if (_vKind = 'TransactionWH') or  (_vKind = 'TransactionALL') then
      begin
        //titolo della form
        Self.Caption:='Transactions for item: '+MainFRM.tblMain.FieldByName('tblArt').AsString;
        //configurazione della listbox
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'Date';
        _lCol.Width := -1;
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'WH';
        _lCol.Width := -1;
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'TT';
        _lCol.Width := -1;
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'Qty';
        _lCol.Width := -1;
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'Cost';
        _lCol.Width := -1;
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'RunSum';
        _lCol.Width := -1;
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'Order';
        _lCol.Width := -1;
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'Reference';
        _lCol.Width := -1;
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'Memo';
        _lCol.Width := -1;
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'User';
        _lCol.Width := -1;
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'DateIns';
        _lCol.Width := -1;
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'HHIns';
        _lCol.Width := -1;

        //preparazione recupero dati iScala
        _lSQLString:=' SELECT SC07002, SC07009, SC07001, SC07004, SC07005, SC07007, SC07006, ' +
                     ' SC07015, SC07037, SC07035, SC07036, SL01002, PL01002 '+
                     ' FROM SC07#AZ#00 LEFT OUTER JOIN ' +
                     ' PL014200 ON SC07006 = PL01001 LEFT OUTER JOIN '+
                     ' SL014200 ON SC07006 = SL01001'+
                     ' WHERE SC07003 = '''+ MainFRM.tblMain.FieldByName('tblArt').AsString+'''';

        //aggiunta filtro su singolo articolo
        if (_vKind = 'TransactionWH') then
          _lSQLString := _lSQLString + ' AND SC07009 = '''+MainFRM.tblMain.FieldByName('tblMag').AsString+'''';

        //ordinamento
        _lSQLString :=_lSQLString + ' ORDER BY SC07002';

        //replacing codice company
        _lSQLString := MainFRM._MultiReplace(_lSQLString, '#AZ#', _lCompanyCode);

        //attivazione qry
        if MainFRM.qryScala.Active then MainFRM.qryScala.Active:=False;
        MainFRM.qryScala.SQL.Clear;
        MainFRM.qryScala.SQL.Add(_lSQLString);
        MainFRM.qryScala.Active:=True;
        if MainFRM.qryScala.RecordCount > 0 then
          begin
            //ciclo per inserimento dati nella listview
            while not MainFRM.qryScala.Eof do
              begin
              _lRunSum := _lRunSum + MainFRM.qryScala.FieldByName('SC07004').AsInteger;
              _lRow:=lvMain.Items.Add;
              _lRow.Caption:=MainFRM.qryScala.FieldByName('SC07002').AsString;
              _lRow.SubItems.Add(MainFRM.qryScala.FieldByName('SC07009').AsString);
              _lRow.SubItems.Add(MainFRM.qryScala.FieldByName('SC07001').AsString);
              _lRow.SubItems.Add(MainFRM.qryScala.FieldByName('SC07004').AsString);
              _lRow.SubItems.Add(MainFRM.qryScala.FieldByName('SC07005').AsString);
              _lRow.SubItems.Add(IntToStr(_lRunSum));
              _lRow.SubItems.Add(MainFRM.qryScala.FieldByName('SC07007').AsString);
              _lRow.SubItems.Add(MainFRM.qryScala.FieldByName('SC07006').AsString +'-'+
                                 MainFRM.qryScala.FieldByName('SL01002').AsString +
                                 MainFRM.qryScala.FieldByName('PL01002').AsString);
              _lRow.SubItems.Add(MainFRM.qryScala.FieldByName('SC07015').AsString);
              _lRow.SubItems.Add(MainFRM.qryScala.FieldByName('SC07037').AsString);
              _lRow.SubItems.Add(MainFRM.qryScala.FieldByName('SC07035').AsString);
              _lRow.SubItems.Add(MainFRM.qryScala.FieldByName('SC07036').AsString);
              MainFRM.qryScala.Next;
              end;
          end;
        MainFRM.qryScala.Active:=False;
      end; //_vKind = 'Transaction*'

    if (_vKind = 'FlussoWH') or (_vKind = 'FlussoALL') then
      begin
        //titolo della form

        Self.Caption:='In/Out/Expected for item: '+MainFRM.tblMain.FieldByName('tblArt').AsString;
        //configurazione della listbox
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'Date';
        _lCol.Width := -1;
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'Req Date';
        _lCol.Width := -1;
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'Order Nr.';
        _lCol.Width := -1;
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'Code';
        _lCol.Width := -1;
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'Company';
        _lCol.Width := -1;
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'WH';
        _lCol.Width := -1;
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'Qty';
        _lCol.Width := -1;
        _lCol:=lvMain.Columns.Add;
        _lCol.Caption := 'RunSum';
        _lCol.Width := -1;

        //calcolo la giacenza alla data attuale
        _lSQLString:=' SELECT SUM(SC07004) AS QTY FROM SC07#AZ#00 ' +
                     ' WHERE SC07003 = '''+ MainFRM.tblMain.FieldByName('tblArt').AsString+'''';

        if (_vKind = 'FlussoWH') then
          _lSQLString := _lSQLString + ' AND SC07009 = ''' + MainFRM.tblMain.FieldByName('tblMag').AsString + '''';

        //replacing codice company
        _lSQLString := MainFRM._MultiReplace(_lSQLString, '#AZ#', _lCompanyCode);

        //attivazione qry
        if MainFRM.qryScala.Active then MainFRM.qryScala.Active:=False;
        MainFRM.qryScala.SQL.Clear;
        MainFRM.qryScala.SQL.Add(_lSQLString);
        MainFRM.qryScala.Active:=True;
        if MainFRM.qryScala.RecordCount > 0 then
          _lRunSum:=MainFRM.qryScala.FieldByName('QTY').AsInteger;

        _lRow:=lvMain.Items.Add;
        _lRow.Caption:=DateToStr(Now());
        _lRow.SubItems.Add('');
        _lRow.SubItems.Add('');
        _lRow.SubItems.Add('Current Qty');
        _lRow.SubItems.Add('');
        _lRow.SubItems.Add('');
        _lRow.SubItems.Add('');
        _lRow.SubItems.Add(IntToStr(_lRunSum));
        //chiudo la qry
        MainFRM.qryScala.Active:=False;


        //recupero dati iScala dal flusso degli articoli
        _lSQLString:=' SELECT SC11002, SC11008, SC11003, SC11006, SC11007, SC11004, SC11005 ' +
                     ' FROM SC11#AZ#00 '+
                     ' WHERE SC11001 = '''+ MainFRM.tblMain.FieldByName('tblArt').AsString+'''';

        //aggiunta filtro su singolo articolo
        if (_vKind = 'FlussoWH') then
          _lSQLString := _lSQLString + ' AND SC11004 = ''' + MainFRM.tblMain.FieldByName('tblMag').AsString + '''';

        //ordinamento
        _lSQLString := _lSQLString + ' ORDER BY SC11002';

        //replacing codice company
        _lSQLString := MainFRM._MultiReplace(_lSQLString, '#AZ#', _lCompanyCode);

        //attivazione qry
        if MainFRM.qryScala.Active then MainFRM.qryScala.Active:=False;
        MainFRM.qryScala.SQL.Clear;
        MainFRM.qryScala.SQL.Add(_lSQLString);
        MainFRM.qryScala.Active:=True;
        if MainFRM.qryScala.RecordCount > 0 then
          begin
            //ciclo per inserimento dati nella listview
            while not MainFRM.qryScala.Eof do
              begin
              _lRow:=lvMain.Items.Add;
              _lRunSum:=_lRunSum + MainFRM.qryScala.FieldByName('SC11005').AsInteger;
              _lRow.Caption:=MainFRM.qryScala.FieldByName('SC11002').AsString;
              _lRow.SubItems.Add(MainFRM.qryScala.FieldByName('SC11008').AsString);
              _lRow.SubItems.Add(MainFRM.qryScala.FieldByName('SC11003').AsString);
              _lRow.SubItems.Add(MainFRM.qryScala.FieldByName('SC11006').AsString);
              _lRow.SubItems.Add(MainFRM.qryScala.FieldByName('SC11007').AsString);
              _lRow.SubItems.Add(MainFRM.qryScala.FieldByName('SC11004').AsString);
              _lRow.SubItems.Add(MainFRM.qryScala.FieldByName('SC11005').AsString);
              _lRow.SubItems.Add(IntToStr(_lRunSum));
              MainFRM.qryScala.Next;
              end;
          end;
        //lvMain.;
        MainFRM.qryScala.Active:=False;
      end;  //_vKind = 'Flusso'

  end;
//----------------------------------------------------------------------------------------------------//
procedure TInfoFRM.FormShow(Sender: TObject);
  begin
    //al momento di mostrare la form eseguo il cariamento dei dati
    _FillData;
  end;
//----------------------------------------------------------------------------------------------------//
procedure TInfoFRM.lvMainCustomDrawSubItem(Sender: TCustomListView;
  Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  var DefaultDraw: Boolean);

  const
           cStripe = $CCFFCC;  // colour of alternate list items
  begin
    //impostazione epr il colore alternato delle righe
    if Odd(Item.Index) then
    // odd list items have green background
      lvMain.Canvas.Brush.Color := cStripe
    else
    // even list items have window colour background
      LvMain.Canvas.Brush.Color := clWindow;
end;
//----------------------------------------------------------------------------------------------------//

end.
